package com.cdc.dp;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

public class ScreenAdaptation {
    private ScreenAdaptation() {
    }

    private static SharedPreferences spScreen = null;
    private static DisplayMetrics metrics;

    private static Application mApplication;

    public static void initialize(Application application) {
        if (spScreen == null){
            spScreen = application.getSharedPreferences("screenSp", 0);
        }
        mApplication=application;
        getScreenAndModel();

    }

    public static String getScreenAndModel() {
        metrics = mApplication.getResources().getDisplayMetrics();
        return getScreen(metrics,mApplication);
    }




    public static String getDensity(){
        return spScreen.getString("density","");
    }

    public static String getWidthPixels(){
        return spScreen.getString("widthPixels","");
    }


    public static String getHeightPixels(){
        return spScreen.getString("heightPixels","");
    }

    public static String getBaseDpi(){
        return spScreen.getString("baseDpi","");
    }


    public static String getRealDpi(){
        return spScreen.getString("realDpi","");
    }

    public static String getVersionName(){
        return spScreen.getString("versionName","");
    }


    public static String getVersionCode(){
        return spScreen.getString("versionCode","");
    }

    public static float dpToPx(float dp){
        float px=dp*metrics.density+0.5f;
        return px;
    }

    public static float pxToDp(float px){
        float dp=px/(metrics.density)+0.5f;
        return dp;
    }

    private static String getScreen(DisplayMetrics metrics,Application application){
        String string="";
        float fl=metrics.density;
        int dpi=metrics.densityDpi;
        int heightPixels=metrics.heightPixels;
        int widthPixels=metrics.widthPixels;
        float scaledDensity=metrics.scaledDensity;
        float xdpi=metrics.xdpi;
        float ydpi=metrics.ydpi;
        float xdp=widthPixels/fl;
        float ydp=heightPixels/fl;
        try {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("density",String.valueOf(fl));
            jsonObject.put("dpi",String.valueOf(dpi));
            jsonObject.put("heightPixels",String.valueOf(heightPixels));
            jsonObject.put("widthPixels",String.valueOf(widthPixels));
            jsonObject.put("scaledDensity",String.valueOf(scaledDensity));
            jsonObject.put("xdpi",String.valueOf(xdpi));
            jsonObject.put("ydpi",String.valueOf(ydpi));
            jsonObject.put("xdp",String.valueOf(xdp));
            jsonObject.put("ydp",String.valueOf(ydp));
            jsonObject.put("os",getSystemVersion());
            jsonObject.put("api",getSystemApi());
            jsonObject.put("brand",getDeviceBrand());
            jsonObject.put("model",getSystemModel());
            spScreen.edit().putString("os",getSystemVersion()).apply();
            spScreen.edit().putString("api",getSystemApi()).apply();
            try {
                PackageInfo info = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
                String version = info.versionName;
                int versionCode=info.versionCode;
                jsonObject.put("versionName",version);
                jsonObject.put("versionCode",String.valueOf(versionCode));
                String baseDpi=application.getResources().getString(R.string.base_dpi);
                jsonObject.put("baseDpi",baseDpi);
                float dp=widthPixels/fl+0.5f;
                jsonObject.put("realDpi",String.valueOf(dp));
                spScreen.edit().putString("baseDpi",baseDpi).apply();
                spScreen.edit().putString("realDpi",String.valueOf(dp)).apply();
                spScreen.edit().putString("versionName",version).apply();
                spScreen.edit().putString("versionCode",String.valueOf(versionCode)).apply();
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            string=jsonObject.toString();
            spScreen.edit().putString("density",String.valueOf(fl)).apply();
            spScreen.edit().putString("widthPixels",String.valueOf(widthPixels)).apply();
            spScreen.edit().putString("heightPixels",String.valueOf(heightPixels)).apply();
            Log.e("screen",string);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return string;
    }



    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }


    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    public static String getSystemApi() {
        return Build.VERSION.SDK_INT+"";
    }

    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取手机厂商
     *
     * @return  手机厂商
     */
    public static String getDeviceBrand() {
        return android.os.Build.BRAND;
    }
}
